import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Query,
  ParseIntPipe,
  DefaultValuePipe
} from '@nestjs/common';
import { AttachementService } from './attachement.service';
import { CreateAttachementDto } from './dto/create-attachement.dto';
import { UpdateAttachementDto } from './dto/update-attachement.dto';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Attachement } from './entities/attachement.entity';
import {
  FileFieldsInterceptor,
  FileInterceptor,
  FilesInterceptor
} from '@nestjs/platform-express';
import { AttachementSize } from './pipe/attachement-size.pipe';
import { attachementQueryVo } from '@/common/interface';

@ApiTags('附件管理')
@Controller('attachement')
export class AttachementController {
  constructor(private readonly attachementService: AttachementService) {}

  @ApiOperation({ summary: '附件列表' })
  @ApiResponse({ status: 200, description: '200', type: [Attachement] })
  @ApiQuery({
    name: 'filename',
    description: '文件原始名称',
    type: String,
  })
  @ApiQuery({
    name: 'queryDate',
    description: '查询时间',
    type: Array
  })
  @ApiQuery({
    name: 'minSize',
    description: '文件大小(起)',
    type: Number,
  })
  @ApiQuery({
    name: 'maxSize',
    description: '文件大小(终)',
    type: Number,
  })
  @ApiQuery({
    name: 'page',
    required: false,
    description: '第几页',
    type: Number,
    example: 1
  })
  @ApiQuery({
    name: 'limit',
    required: false,
    description: '每页显示数',
    type: Number,
    example: 10
  })

  @Get()
  findAll(
    @Query() query: attachementQueryVo,
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page = 1,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit = 10
  ) {
    return this.attachementService.findAll({ page, limit }, query);
  }

  @ApiOperation({ summary: '图片上传' })
  @ApiResponse({ status: 200, description: '200', type: Attachement })
  @Post('img')
  @UseInterceptors(FileInterceptor('file'))
  uploadImg(@UploadedFile(AttachementSize) file: Express.Multer.File) {
    return this.attachementService.upload(file);
  }

  @ApiOperation({ summary: '单附件上传' })
  @ApiResponse({ status: 200, description: '200', type: Attachement })
  @Post('file')
  @UseInterceptors(FileInterceptor('file')) // 使用文件拦截器
  upload(@UploadedFile() file: Express.Multer.File) {
    return this.attachementService.upload(file);
  }

  @ApiOperation({ summary: '多附件上传' })
  @ApiResponse({ status: 200, description: '200', type: Attachement })
  @Post('files')
  @UseInterceptors(FilesInterceptor('files'))
  uploads(@UploadedFiles() files: Express.Multer.File[]) {
    console.log(files);
    return files;
  }
  @ApiOperation({ summary: '多字段的附件上传' })
  @ApiResponse({ status: 200, description: '200', type: Attachement })
  @Post('fieldFile')
  @UseInterceptors(
    FileFieldsInterceptor([
      {
        name: 'prop1'
      },
      {
        name: 'prop2'
      }
    ])
  )
  uploadsField(
    @UploadedFiles()
    files: {
      prop1?: Express.Multer.File[];
      prop2?: Express.Multer.File[];
    }
  ) {
    console.log(files);
    return files;
  }
}
